package org.spring.springboot.app.adv.advmobilerightinfo.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spring.springboot.app.adv.advarea.dao.AdvAreaDao;
import org.spring.springboot.app.adv.advexam.domain.po.AdvExamPO;
import org.spring.springboot.app.adv.advexam.service.IAdvExamService;
import org.spring.springboot.app.adv.advfamilylockinfo.domain.vo.AdvFamilyLockInfoSaveOutVO;
import org.spring.springboot.app.adv.advinfo.dao.AdvInfoDao;
import org.spring.springboot.app.adv.advinfo.domain.po.AdvInfoPO;
import org.spring.springboot.app.adv.advmobilerightinfo.dao.AdvMobileRightInfoDao;
import org.spring.springboot.app.adv.advmobilerightinfo.domain.po.AdvMobileRightInfoPO;
import org.spring.springboot.app.adv.advmobilerightinfo.domain.vo.*;
import org.spring.springboot.app.adv.advmobilerightinfo.service.IAdvMobileRightInfoService;
import org.spring.springboot.app.agent.balancelog.service.ISysOfficeBalanceLogService;
import org.spring.springboot.app.base.Constants;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.base.User;
import org.spring.springboot.app.sys.office.service.ISysOfficeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AdvMobileRightInfoServiceImpl implements IAdvMobileRightInfoService {

    private static final Logger LOGGER = LoggerFactory.getLogger(AdvMobileRightInfoServiceImpl.class);

    @Autowired
    private AdvMobileRightInfoDao advMobileRightInfoDao;
    @Autowired
    private AdvInfoDao advInfoDao;
    @Autowired
    private IAdvExamService advExamService;


    @Override
    public AdvMobileRightInfoPO find(String id) {
        return advMobileRightInfoDao.selectByPrimaryKey(id);
    }

    @Override
    public R delete(String id) {
        R r = new R();
        AdvInfoPO mainPo = new AdvInfoPO();
        mainPo.setId(id);
        mainPo.preDelete();
        if (advInfoDao.updateByPrimaryKeySelective(mainPo) == 0) {
            return r.fail().setMessage("保存失败");
        }
        AdvMobileRightInfoPO po = new AdvMobileRightInfoPO();
        po.setId(id);
        po.preDelete();
        if (advMobileRightInfoDao.updateByPrimaryKeySelective(po) == 0) {
            return r.fail().setMessage("保存失败");
        }
        return r.success();
    }


    @Override
    public List<AdvMobileRightInfoPO> findPage(AdvMobileRightInfoSearchVO searchVO, int pageNum, int pageSize) {
        return advMobileRightInfoDao.findPage(searchVO, pageNum,pageSize);
    }

    @Override
    @Transactional
    public R saveInfo(AdvMobileRightInfoSaveInVO vo, User user) {
        R r = new R();
        //保存主表
        AdvInfoPO mainPO = new AdvInfoPO();
        mainPO.setOfficeId(user.getOfficeId());
        mainPO.setType(Constants.ADV_INFO_TYPE_2);
        mainPO.setIsSystem(0);
        mainPO.setTitle(vo.getTitle());
        mainPO.setPhotos(vo.getPhotos());
        mainPO.setStartDate(vo.getStartDate_());
        mainPO.setEndDate(vo.getEndDate_());
        mainPO.setPayState(Constants.ADV_INFO_TYPE_PAY_STATE_1);
        mainPO.setPayMo(null);
        mainPO.setContent(vo.getContent());
        mainPO.preInsert();
        if (advInfoDao.insertSelective(mainPO) <1) {
            throw new RuntimeException("保存活动主表失败");
        }
        //保存副表
        AdvMobileRightInfoPO mobileRightInfoPO = new AdvMobileRightInfoPO();
        mobileRightInfoPO.setShowPosition(vo.getShowPosition());
        mobileRightInfoPO.setParentId(vo.getParentId());
        mobileRightInfoPO.setUrl(vo.getUrl());
        mobileRightInfoPO.setSort(vo.getSort());
        mobileRightInfoPO.setViewCount(0);
        mobileRightInfoPO.preInsert();
        mobileRightInfoPO.setId(mainPO.getId());
        if (advMobileRightInfoDao.insertSelective(mobileRightInfoPO) <1) {
            throw new RuntimeException("保存活动副表失败");
        }
        AdvMobileRightInfoSaveOutVO outVO = new AdvMobileRightInfoSaveOutVO();
        outVO.setId(mainPO.getId());
        return r.success().setData(outVO);
    }




    @Override
    public R updateInfo(AdvMobileRightInfoUpdateInVO vo) {
        R r = new R();
        //判断广告是否审核（审核之前可以修改）
        AdvMobileRightInfoPO mobileRightInfoPO = advMobileRightInfoDao.selectByPrimaryKey(vo.getId());
        if (mobileRightInfoPO.getExamState() != null && mobileRightInfoPO.getExamState().intValue() == Constants.ADV_EXAM_STATE_2) {
            return r.fail().setMessage("已审核广告不能修改");
        }

        //保存主表
        AdvInfoPO mainPo = new AdvInfoPO();
        mainPo.setId(vo.getId());
        mainPo.setTitle(vo.getTitle());
        mainPo.setContent(vo.getContent());
        mainPo.setPhotos(vo.getPhotos());
        mainPo.preUpdate();
        if (advInfoDao.updateByPrimaryKeySelective(mainPo) <1) {
            return r.fail().setMessage("修改活动主表失败");
        }
        AdvMobileRightInfoPO mobileRightInfoPO1 = new AdvMobileRightInfoPO();
        mobileRightInfoPO1.setId(vo.getId());
        mobileRightInfoPO1.setShowPosition(vo.getShowPosition());
        mobileRightInfoPO1.setParentId(vo.getParentId());
        mobileRightInfoPO1.setUrl(vo.getUrl());
        mobileRightInfoPO1.setSort(vo.getSort());
        mobileRightInfoPO1.preUpdate();
        if (advMobileRightInfoDao.updateByPrimaryKeySelective(mobileRightInfoPO1) <1) {
            return r.fail().setMessage("修改活动副表失败");
        }
        return r.success();
    }


    @Override
    @Transactional
    public R pay(String id, User user) {
        R r = new R();
        AdvInfoPO mainPO = advInfoDao.selectByPrimaryKey(id);
        //判断支付状态
        if (mainPO.getPayState().intValue() == Constants.ADV_INFO_TYPE_PAY_STATE_2) {
            return r.fail().setMessage("活动已经支付");
        }
        //添加审核表
        advExamService.addExam(user.getOfficeId(),id);
        //修改广告状态
        mainPO.setPayState(Constants.ADV_INFO_TYPE_PAY_STATE_2);
        mainPO.preUpdate();
        if (advInfoDao.updateByPrimaryKeySelective(mainPO) < 1) {
            throw new RuntimeException("更新活动主表失败");
        }
        return r.success();
    }

    @Override
    public R saveSystemInfo(AdvMobileRightInfoSystemSaveInVO vo, User user) {
        R r = new R();
        //保存主表
        AdvInfoPO mainPO = new AdvInfoPO();
        mainPO.setOfficeId(user.getOfficeId());
        mainPO.setType(Constants.ADV_INFO_TYPE_2);
        mainPO.setIsSystem(1);
        mainPO.setTitle(vo.getTitle());
        mainPO.setPhotos(vo.getPhotos());
        mainPO.setStartDate(vo.getStartDate_());
        mainPO.setEndDate(vo.getEndDate_());
        mainPO.setPayState(Constants.ADV_INFO_TYPE_PAY_STATE_0);
        mainPO.setPayMo(null);
        mainPO.setContent(vo.getContent());
        mainPO.preInsert();
        if (advInfoDao.insertSelective(mainPO) <1) {
            throw new RuntimeException("保存活动主表失败");
        }
        //保存副表
        AdvMobileRightInfoPO mobileRightInfoPO = new AdvMobileRightInfoPO();
        mobileRightInfoPO.setShowPosition(vo.getShowPosition());
        mobileRightInfoPO.setParentId(vo.getParentId());
        mobileRightInfoPO.setUrl(vo.getUrl());
        mobileRightInfoPO.setSort(vo.getSort());
        mobileRightInfoPO.setViewCount(0);
        mobileRightInfoPO.preInsert();
        mobileRightInfoPO.setId(mainPO.getId());
        if (advMobileRightInfoDao.insertSelective(mobileRightInfoPO) <1) {
            throw new RuntimeException("保存活动副表失败");
        }
        return r.success();
    }


}
